var axios = require('axios');
var fs = require('fs');
var path = require('path');
var downFile = require('./downFile');
var cheerio = require('cheerio');

module.exports = {
  downFile
};

function downXiaoshuo() {
  let url = 'http://www.audio699.com/book/807/76.html';
}

function downOneYinpin(index) {
  axios({
    url: getXiaoshuoHtmlUrl(index)
  }).then(res => {
    var $ = cheerio.load(res.data, {
      decodeEntities: false
    });
    let url = $('audio>source')[0].attribs.src;
    console.log('下载小说:', index);
    downFile(url, '音频/' + index + '.m4a').then(res => {
      console.log('下载小说完成----->:', index);
    });
  });
}
function getXiaoshuoHtmlUrl(index) {
  return 'http://www.audio699.com/book/807/' + index + '.html';
}

let end = 390;
let begin = 106;
function down() {
  if (begin > end) {
    return;
  }
  downOneYinpin(begin);
  setTimeout(() => {
    ++begin;
    down();
  }, 1000 * 60 * 10 + Math.random() * 100000);
}
down();
